﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;

namespace auxiliary_proj
{
    public partial class FormViewUser : Form
    {
        public FormViewUser()
        {
            InitializeComponent();
        }
        //server connection stuff
        private MySqlConnection conn;
        private MySqlDataAdapter adapt;
        DataSet ds;
        int row = 0;
        MemoryStream ms;
        byte[] image_array;

        private void FormViewUser_Load(object sender, EventArgs e)
        {
            //parse the server file
            Program.parseServerInfo();

            //establish a connection to the server.
            conn = new MySqlConnection(Program.getConnection());
            Cursor.Current = Cursors.WaitCursor;
            loadData();
            displayData();
            conn.Close();
            Cursor.Current = Cursors.Default;
        }

        public void loadData()
        {
            string query = "SELECT first_name, last_name, email, photo from USER";
            adapt = new MySqlDataAdapter(query, conn);
            adapt.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            ds = new DataSet();
            adapt.Fill(ds, "USER");
            
        }

        public void displayData()
        {
            
            if (ds.Tables[0].Rows.Count > 0)
            {
                lbUserFirstName.Text = ds.Tables[0].Rows[row][0].ToString();
                lbUserLastName.Text = ds.Tables[0].Rows[row][1].ToString();
                lbUserEmail.Text = ds.Tables[0].Rows[row][2].ToString();
                pbViewEmp.Image = null;
                if (ds.Tables[0].Rows[row][3] != System.DBNull.Value)
                {
                    image_array = (byte[])ds.Tables[0].Rows[row][3];
                    ms = new MemoryStream(image_array);
                    Image img = Image.FromStream(ms);
                    pbViewEmp.Image = img;
                }
            }
            else MessageBox.Show("No Record Available");
        }

        private void llbNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (row < ds.Tables[0].Rows.Count - 1)
            {
                row++;
                displayData();
            }
            else
            {
                row = 0;
                displayData();
            }
        }

        private void llbPrev_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (row > 0)
            {
                row--;
                displayData();
            }
            else
            {
                row = ds.Tables[0].Rows.Count-1;
                displayData();
            }
        }
    }
}
